// 统一封装接口方法
import axios from '@/utils/request.js'
import { getToken } from '@/utils/token'

// 登录接口
export const loginApi = (data) => {
  return axios({
    url: '/v1_0/authorizations',
    method: 'POST',
    data
  })
}

// 获取所有频道
export const getAllChannelsAPI = () => {
  return axios({
    url: '/v1_0/channels',
    method: 'GET'
  })
}

// 获取用户选择的频道
export const getUserChannelsAPI = () => {
  return axios({
    url: '/v1_0/user/channels',
    method: 'GET',
    headers: {
      Authorization: `Bearer ${getToken()}`
    }
  })
}

// 获取列表数据
export const getAllArticleListAPI = ({
  channel_id,
  timestamp
}) => {
  return axios({
    url: '/v1_0/articles',
    method: 'GET',
    headers: {
      Authorization: `Bearer ${getToken()}`
    },
    params: {
      channel_id,
      timestamp
    }
  })
}

// 不感兴趣的接口
export const dislikeArticleAPI = (id) => {
  return axios({
    url: '/v1_0/article/dislikes',
    method: 'POST',
    headers: {
      Authorization: `Bearer ${getToken()}`
    },
    data: {
      target: id
    }
  })
}

// 举报文章
export const reportArticleAPI = (id, value) => {
  return axios({
    url: '/v1_0/article/reports',
    method: 'POST',
    headers: {
      Authorization: `Bearer ${getToken()}`
    },
    data: {
      target: id,
      type: value,
      remark: '其他问题'
    }
  })
}

// 更新用户频道
export const updateChannelsAPI = (channels) => {
  return axios({
    url: '/v1_0/user/channels',
    method: 'PUT',
    data: {
      channels
    }
  })
}

// 获取搜索联想菜单
export const suggestListAPI = (q) => {
  if (q) {
    return axios({
      url: '/v1_0/suggestion?',
      params: {
        q
      }
    })
  }
}

// 搜索-- 搜索结果列表
export const searchResultAPI = (q) => {
  return axios({
    url: '/v1_0/search',
    params: {
      q
    }
  })
}

// 文章  获取详情
export const articleDetailAPI = (id) => {
  return axios({
    url: `/v1_0/articles/${id}`
  })
}

// 文章  关注作者
export const userFollowedAPI = (target) => {
  return axios({
    url: '/v1_0/user/followings',
    method: 'POST',
    data: {
      target
    }
  })
}

// 文章  取消关注作者
export const userUnFollowedAPI = (id) => {
  return axios({
    url: `/v1_0/user/followings/${id}`,
    method: 'DELETE'
  })
}

// 文章  点赞
export const likeArticleAPI = (target) => {
  return axios({
    url: '/v1_0/article/likings',
    method: 'POST',
    data: {
      target
    }
  })
}

// 文章  取消点赞
export const unLikeArticleAPI = (id) => {
  return axios({
    url: `/v1_0/article/likings/${id}`,
    method: 'DELETE'
  })
}

// 文章 获取评论列表
export const commentsListAPI = ({ id, offset = null, limt = 10 }) => {
  return axios({
    url: '/v1_0/comments',
    params: {
      type: 'a',
      source: id,
      offset,
      limt
    }
  })
}

// 评论 - 喜欢
export const commentLikingAPI = (comId) => {
  return axios({
    url: '/v1_0/comment/likings',
    method: 'POST',
    data: {
      target: comId
    }
  })
}

// 评论-取消喜欢
export const commentDisLikingAPI = (comId) => {
  return axios({
    url: `/v1_0/comment/likings/${comId}`,
    method: 'DELETE'
  })
}

// 评论 - 发布
export const sendCommentAPI = ({
  artId,
  content
}) => {
  return axios({
    url: '/v1_0/comments',
    method: 'POST',
    data: {
      target: artId,
      content: content
    }
  })
}

// 用户 - 基本资料
export const userInfoAPI = () => {
  return axios({
    url: '/v1_0/user'
  })
}

// 用户- 个人资料(就为了获取生日)
export const userProfileAPI = () => {
  return axios({
    url: '/v1_0/user/profile'
  })
}

// 用户- 更新头像
// 注意: formObj的值必须是一个表单对象
// '{"a": 10, "b": 20}' // 对象格式的JSON字符串
// new FormData() // 表单对象
export const updatePhotoAPI = (formObj) => {
  return axios({
    url: '/v1_0/user/photo',
    method: 'PATCH',
    data: formObj
    // 如果你的请求体内容是表单对象, 浏览器会自动携带请求头Content-Type为multipart/form-data
  })
}

// 用户_更新资料
export const updateprofileAPI = ({
  birthday, name
}) => {
  return axios({
    url: '/v1_0/user/profile',
    method: 'PATCH',
    data: {
      birthday,
      name
    }
  })
}

// 用户 - 更新token
export const refreshTokenAPI = () => {
  return axios({
    url: '/v1_0/authorizations',
    method: 'PUT',
    headers: {
      Authorization: `Bearer ${localStorage.getItem('refresh_token')}`
    }
  })
}
